﻿using FlyingEye.Consts;
using FlyingEye.DeviceMaintenance;
using FlyingEye.Factories;
using Microsoft.EntityFrameworkCore;

namespace FlyingEye.EntityFrameworkCore
{
    public static class FactoryModelBuilder
    {
        public static void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 基地
            modelBuilder.Entity<ProductionBaseModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ProductionBases",
                 DeviceOpsServiceDbConsts.DbSchema);

                // 基地名称不能为空
                builder.Property(e => e.SiteName)
                .IsRequired();

                // 基地名称唯一
                builder.HasIndex(e => e.SiteName)
                .IsUnique();

                // 一个基地可以有多个车间
                builder.HasMany<ProductionWorkshopModel>()
                .WithOne()
                .HasForeignKey(e => e.SiteId)
                .IsRequired();
            });

            // 车间
            modelBuilder.Entity<ProductionWorkshopModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ProductionWorkshops",
                  DeviceOpsServiceDbConsts.DbSchema);

                builder.HasMany<ProductionProcessModel>()
                .WithOne()
                .HasForeignKey(e => e.ProductionWorkshopId)
                .IsRequired();

                // 工厂名称不能为空
                builder.Property(e => e.WorkshopName)
                .IsRequired();

                // 基地编号和工厂名称唯一，例如 hx: 2000 - hc01
                builder.HasIndex(e => new { e.SiteId, e.WorkshopName })
                .IsUnique();
            });

            // 生产工序
            modelBuilder.Entity<ProductionProcessModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "ProductionProcesses",
                  DeviceOpsServiceDbConsts.DbSchema);

                // 一个工序可以有多个设备主机
                builder.HasMany<DeviceHostModel>()
                .WithOne()
                .HasForeignKey(e => e.ProductionProcessId)
                .IsRequired();

                // 工序名称不能为空
                builder.Property(e => e.ProcessName)
                .IsRequired();

                // 工序名称要唯一
                builder.HasIndex(e => new { e.ProductionWorkshopId, e.ProcessName })
                .IsUnique();
            });

            // 设备主机
            modelBuilder.Entity<DeviceHostModel>(builder =>
            {
                builder.ToTable(DeviceOpsServiceDbConsts.DbTablePrefix + "DeviceHosts",
                  DeviceOpsServiceDbConsts.DbSchema);

                // 一个设备主机有多个需要维护的设备，例如：中控下面会连接多个不同设备资源号的 PLC 设备
                builder.HasMany<DeviceMaintenanceModel>()
                .WithOne()
                .HasForeignKey(e => e.DeviceHostId)
                .IsRequired();

                //  设备主机名称不能为空
                builder.Property(e => e.DeviceHostName)
                .IsRequired();

                // 设备主机名称要唯一
                builder.HasIndex(e => new { e.ProductionProcessId, e.DeviceHostName });
            });

        }
    }
}
